package ciyuan.Block.Machine.Simple.Smelter;

import ciyuan.Block.Machine.Simple.SimpleMachineRegister;
import ciyuan.Block.TypeInterface.MachineTileEntity;
import ciyuan.Item.ItemRegisterManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class SmelterTileEntity extends MachineTileEntity {

    private static final ITextComponent TITLE = new TranslationTextComponent("block.ciyuanrpg.smelter");

    public SmelterTileEntity() {
        super(SimpleMachineRegister.SMELTER_TILE_ENTITY.get(), 4, TITLE, 800);
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new SmelterContainer(SimpleMachineRegister.SMELTER_CONTAINER.get(), sycID, inventory, getBlockPos());
    }

    @Override
    public void tick() {
        if (!this.hasLevel() || this.level.isClientSide) {
            return;
        }
        ItemStack copperItemStack = this.inventory.getItem(0);
        ItemStack tinItemStack = this.inventory.getItem(1);
        ItemStack fuelItemStack = this.inventory.getItem(2);

        if (copperItemStack.isEmpty() || tinItemStack.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (!copperItemStack.getItem().equals(ItemRegisterManager.Copper.get()) && !tinItemStack.getItem().equals(ItemRegisterManager.tin.get())) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (this.getProgress() >= this.getConsumeTime()) {
            ItemStack itemStack = new ItemStack(ItemRegisterManager.Tin_Alloy.get());
            if (this.inventory.canAddItem(itemStack)) {
                copperItemStack.setCount(copperItemStack.getCount() - 1);
                tinItemStack.setCount(tinItemStack.getCount() - 1);
                this.inventory.addItem(itemStack);
                this.resetProgress();
            }
        } else if (!this.consumeFuel() && !fuelItemStack.isEmpty() && getFuelAddCount(fuelItemStack.getItem()) != 0) {
            fuelItemStack.setCount(fuelItemStack.getCount() - 1);
            this.addFuel(getFuelAddCount(fuelItemStack.getItem()));
            this.consumeFuel();
            this.setMachineStatus(true);
        }
    }

}
